
const db = require('../serve/db.js')
const connect = require('../utils/connect')
const token = require('../utils/token')
function result(status,data,msg){
    this.status = status;
    this.data = data || {};
    this.msg = msg;
}

async function Commit(req,res){
        let data = {
          token:req.query.token ||1,
          title:req.query.title || 0,
          content: req.query.content || 0,
          CreateTime:Date.parse(new Date())/1000,
          hash:Math.random().toString(36).substr(2,10)
        };
        for(var i in data){
          if(!data[i]){
            res.send(new result(110,'',i+'未填写！'));
            return
          }
        }
        let dbc = await connect();
        let tok = await token(dbc,'token',{token:data.token});
        if(tok){
          let usr = await dbc.db('mongo').collection('member').findOne({uid:tok.uid})
          if(!usr){
            res.send(new result(200,{},'用户不存在'))
            dbc.close()
            return 
          }
          data.creater = usr.member;
          data.head = usr.head;
          data.uid     = usr.uid;
          delete data.token;
          db.insert('Commit',data);
          res.send(new result(200,{},'填写正确'))
        }else{
          dbc.close()
          res.send(new result(101,{},'请登录'))
        }
}

module.exports = Commit